Return to doc.sitecore.com

Valid for Sitecore 5.2, 5.3
How to add a user to the Extranet or Intranet domain programmatically?

Q: Could you give me a short “demo” (code) how to add a user to the Extranet or Intranet domain?

A: Here you are:  

For Sitecore Intranet domain users you should use the following code:

using Sitecore;

using Sitecore.Data;

using Sitecore.Data.Items;

using Sitecore.Configuration;

 

  ...

 

         Database database = Factory.GetDatabase("security");

         Item users = database.Items["/sitecore/users/Administrators"];

         try

         {

            TemplateItem template = database.Templates[TemplateIDs.User];                                    

            Item user = users.Add("TempUser", template);

         }

         catch(Exception ex)

         {

            Response.Write("User cannot be created. Error: "+ ex.Message);

         }

For the Extranet domain the code is very similar (mind the code in bold):

using Sitecore;

using Sitecore.Data;

using Sitecore.Data.Items;

using Sitecore.Configuration;

 

  ...

   

         Database database = Factory.GetDatabase("extranet");

         Item users = database.Items["/sitecore/Users/Customers"];

         try

         {

            TemplateItem template = database.Templates[TemplateIDs.User];                                    

            Item user = users.Add("ExtranetUser", template);

         }

         catch(Exception ex)

         {

            Response.Write("User cannot be created. Error: " + ex.Message);

         }